Passed
Push — master ( 8d4289...fc2d57 )
by Barry
01:09
created

test.js ➔ ... ➔ it(ꞌmdprepare --help worksꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
/* global describe, it */
2
3
 const processFile = require('../dist/processFile.js').default
4
 const {processText} = require('../dist/processFile.js')
5
 const {findCode, findMdpInsert, findMdpCode, replaceLineEndings} = require('../dist/helpers.js')
6
 const {runCliCmd} = require('../dist/processFile.js')
7
 const assert = require('assert')
8
 const {exec} = require('child_process')
9
 const fs = require('fs-extra')
10
 const os = require('os')
11
12
 const mdpLinkTests = [
13
   {name: 'Simple 1', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
14
   {name: 'Simple 2', text: 't\n[> a]: # (mdpInsert abc)\r\n12345\r\n[< b]: #\r\n', result: {start: 2, length: 41, internalStart: 28, internalLength: 5, commandString: 'mdpInsert abc'}},
15
   {name: 'Simple 3', text: '[>]: # (mdpInsert a)\r\n[<]: #\r\n', result: {start: 0, length: 28, internalStart: 22, internalLength: -2, commandString: 'mdpInsert a'}},
16
   {name: 'Simple 4', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
17
   {name: 'Simple 5 (invalid)', text: '[#]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: -1}},
18
   {name: 'code fenced', text: '[>]: # (mdpInsert abc)\r\n12345\r\n`[<]`: #\r\n', result: {start: -1}},
19
   {name: 'embedded', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}},
20
   {name: 'embedded, 5 deep', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 193, internalStart: 24, internalLength: 161, commandString: 'mdpInsert abc'}},
21
   {name: 'embedded, too deep (6) - fails', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: -1}},
22
   {name: 'embedded, some fenced 1', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 250, internalStart: 24, internalLength: 218, commandString: 'mdpInsert abc'}},
23
   {name: 'embedded, some fenced 2', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n[<]: #\r\n', result: {start: 0, length: 52, internalStart: 24, internalLength: 20, commandString: 'mdpInsert abc'}},
24
   {name: 'embedded with following', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n[>]: # (mdpInsert xyz)\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}}
25
 ]
26
27
 const mdpCodeTests = [
28
   {name: 'Simple 1', text: '```json mdpInsert cat e.txt\r\ncode\r\n```', result: {start: 0, length: 38, internalStart: 29, internalLength: 4, commandString: 'json mdpInsert cat e.txt'}},
29
   {name: 'Fails mdpinsert spelt wrong', text: '```json mdpinsert cat e.txt\r\ncode\r\n```', result: {start: -1}},
30
   {name: 'Complex command line', text: 'text\r\n``` json   notes  mdpInsert cat e.txt --option1 sp --option2 \r\ncode\r\n```\r\n', result: {start: 6, length: 72, internalStart: 69, internalLength: 4, commandString: 'json   notes  mdpInsert cat e.txt --option1 sp --option2'}}
31
 ]
32
33
 const fencedCodeTests = [
34
   {name: 'Simple 1', text: 'plain\r\n```\r\ncode\r\n```\r\nplain', result: {start: 7, length: 14, internalStart: 12, internalLength: 4, commandString: ''}},
35
   {name: 'Simple 2', text: 'plain\n```\ncode\n```\nplain', result: {start: 6, length: 12, internalStart: 10, internalLength: 4, commandString: ''}},
36
   {name: 'Example 88', text: '```\n<\n >\n```', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
37
   {name: 'Example 89', text: '~~~\n<\n >\n~~~', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
38
   {name: 'Example 90', text: '``\nfoo\n``', result: {start: 0, length: 9, internalStart: 2, internalLength: 5, commandString: ''}},
39
   {name: 'Example 91', text: '```\naaa\n~~~\n```', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
40
   {name: 'Example 92', text: '~~~\naaa\n```\n~~~', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
41
   {name: 'Example 93', text: '````\naaa\n```\n``````', result: {start: 0, length: 19, internalStart: 5, internalLength: 7, commandString: ''}},
42
   {name: 'Example 94', text: '~~~~\naaa\n~~~\n~~~~', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
43
   {name: 'Example 95', text: '```', result: {start: 0, length: 3, internalStart: 3, internalLength: 0, commandString: ''}},
44
   {name: 'Example 96', text: '`````\n\n```\naaa', result: {start: 0, length: 14, internalStart: 6, internalLength: 8, commandString: ''}},
45
   {name: 'Example 97', text: '> ```\n> aaa\n\nbbb', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
46
   {name: 'Example 97a', text: '> ```\n> aaa\n> \n> bbb', result: {start: 0, length: 20, internalStart: 6, internalLength: 14, commandString: ''}},
47
   {name: 'Example 97b', text: '> ```\r\n> aaa\r\n\r\nbbb', result: {start: 0, length: 12, internalStart: 7, internalLength: 5, commandString: ''}},
48
   {name: 'Example 97c', text: '> ```\r\n> aaa\r\n> \r\n> bbb', result: {start: 0, length: 23, internalStart: 7, internalLength: 16, commandString: ''}},
49
   {name: 'Example 97d', text: '>```a\r\n>\r\nbbb', result: {start: 0, length: 8, internalStart: 7, internalLength: 1, commandString: 'a'}},
50
   {name: 'Example 98', text: '```\n\n  ```', result: {start: 0, length: 10, internalStart: 4, internalLength: 0, commandString: ''}},
51
   {name: 'Example 99', text: '```\n```', result: {start: 0, length: 7, internalStart: 4, internalLength: -1, commandString: ''}},
52
   {name: 'Example 100', text: ' ```\n aaa\naaa\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 8, commandString: ''}},
53
   {name: 'Example 101', text: '  ```\naaa\n  aaa\naaa\n  ```', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
54
   {name: 'Example 101b', text: '  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
55
   {name: 'Example 101c', text: '  ```\naaa\n  aaa\naaa\n  ```\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
56
   {name: 'Example 101d', text: '\r\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 2, length: 25, internalStart: 8, internalLength: 13, commandString: ''}},
57
   {name: 'Example 101e', text: '\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 1, length: 25, internalStart: 7, internalLength: 13, commandString: ''}},
58
   {name: 'Example 102', text: '   ```\n   aaa\n    aaa\n   ```', result: {start: 0, length: 28, internalStart: 7, internalLength: 14, commandString: ''}},
59
   {name: 'Example 103', text: '    ```\n    aaa\n    ```', result: {start: 0, length: 23, internalStart: 0, internalLength: 23, commandString: ''}},
60
   {name: 'Example 104', text: '```\naaa\n  ```', result: {start: 0, length: 13, internalStart: 4, internalLength: 3, commandString: ''}},
61
   {name: 'Example 105', text: '   ```\naaa\n  ```', result: {start: 0, length: 16, internalStart: 7, internalLength: 3, commandString: ''}},
62
   {name: 'Example 106', text: '```\naaa\n    ```', result: {start: 0, length: 15, internalStart: 4, internalLength: 11, commandString: ''}},
63
   {name: 'Example 107', text: '``` ```\naaa', result: {start: 0, length: 7, internalStart: 3, internalLength: 1, commandString: ''}},
64
   {name: 'Example 108', text: '~~~~~~\naaa\n~~~ ~~', result: {start: 0, length: 17, internalStart: 7, internalLength: 10, commandString: ''}},
65
   {name: 'Example 109', text: 'foo\n```\nbar\n```\nbaz', result: {start: 4, length: 11, internalStart: 8, internalLength: 3, commandString: ''}},
66
   {name: 'Example 110', text: 'foo\n---\n~~~\nbar\n~~~\n# baz', result: {start: 8, length: 11, internalStart: 12, internalLength: 3, commandString: ''}},
67
   {name: 'Example 111', text: '```ruby\ndef foo(x)\n  return 3\nend\n```', result: {start: 0, length: 37, internalStart: 8, internalLength: 25, commandString: 'ruby'}},
68
   {name: 'Example 112', text: '~~~~    ruby startline=3 $%@#$\r\ndef foo(x)\r\n  return 3\r\nend\r\n~~~~~~~', result: {start: 0, length: 68, internalStart: 32, internalLength: 27, commandString: 'ruby startline=3 $%@#$'}},
69
   {name: 'Example 113', text: '````;\r\n````', result: {start: 0, length: 11, internalStart: 7, internalLength: -2, commandString: ';'}},
70
   {name: 'Example 114', text: '``` aa ```\r\nfoo', result: {start: 0, length: 10, internalStart: 3, internalLength: 4, commandString: ''}},
71
   {name: 'Example 115', text: '```\r\n``` aaa\r\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
72
   {name: 'Example 324', text: '`foo`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
73
   {name: 'Example 325', text: '`` foo ` bar  ``', result: {start: 0, length: 16, internalStart: 2, internalLength: 12, commandString: ''}},
74
   {name: 'Example 326', text: '` `` `', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
75
   {name: 'Example 327', text: '``\r\nfoo\r\n``', result: {start: 0, length: 11, internalStart: 2, internalLength: 7, commandString: ''}},
76
   {name: 'Example 328', text: '`foo   bar\r\n  baz`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
77
   {name: 'Example 329', text: '`a b`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
78
   {name: 'Example 330', text: '`foo `` bar`', result: {start: 0, length: 12, internalStart: 1, internalLength: 10, commandString: ''}},
79
   {name: 'Example 331', text: '`foo\\`bar`', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
80
   {name: 'Example 332', text: '*foo`*`', result: {start: 4, length: 3, internalStart: 5, internalLength: 1, commandString: ''}},
81
   {name: 'Example 333', text: '[not a `link](/foo`)', result: {start: 7, length: 12, internalStart: 8, internalLength: 10, commandString: ''}},
82
   {name: 'Example 334', text: '`<a href="`">`', result: {start: 0, length: 11, internalStart: 1, internalLength: 9, commandString: ''}},
83
   {name: 'Example 336', text: '`<http://foo.bar.`baz>`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
84
   {name: 'Example 338', text: '```foo``', result: {start: -1}},
85
   {name: 'Example 339', text: '`foo', result: {start: -1}},
86
   {name: 'Example 340', text: '`foo``bar``', result: {start: 4, length: 7, internalStart: 6, internalLength: 3, commandString: ''}},
87
   {name: 'block and code 1', text: '>```\r\n>code\r\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
88
   {name: 'block and code 1b', text: '>```\r\n>code\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
89
   {name: 'block and code 2', text: '>```\r\n>code\r\ntext```\r\n', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
90
   {name: 'block and code 3', text: '>```\r\n>code\r\n>```\r\ntext', result: {start: 0, length: 17, internalStart: 6, internalLength: 5, commandString: ''}}
91
 ]
92
93
 const lineEndingTests = [
94
   {name: '1', text: '\nabc\r\nabc\n\n', resultCRLF: '\r\nabc\r\nabc\r\n\r\n', resultLF: '\nabc\nabc\n\n'},
95
   {name: '2', text: ' \n\nabc\n\rabc\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '},
96
   {name: '3', text: '\r\nabc\nabc\r\n', resultCRLF: '\r\nabc\r\nabc\r\n', resultLF: '\nabc\nabc\n'},
97
   {name: '4', text: ' \r\n\r\nabc\r\n\rabc\r\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '}
98
 ]
99
100
 const processTextTests = [
101
   {
102
     name: 'Simple 1',
103
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
104
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
105
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
106
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
107
   }, {
108
     name: 'Simple 1a',
109
     text: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n\r\n12345\r\n[<]: #\r\n',
110
     clear: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n[<]: #\r\n',
111
     full_win: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n```\r\nabc\r\n```\r\n[<]: #\r\n',
112
     full_linux: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n```\r\nabc\r\n```\r\n[<]: #\r\n'
113
   }, {
114
     name: 'mdpInsert command not present',
115
     text: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
116
     clear: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
117
     full_win: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
118
     full_linux: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n'
119
   }, {
120
     name: 'Command Line invalid',
121
     text: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
122
     clear: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n[<]: #\r\n',
123
     full_win: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n[<]: #\r\n',
124
     full_linux: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n/bin/sh: 1: catt: not found\r\n[<]: #\r\n'
125
   }, {
126
     name: 'Surrounded',
127
     text: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nold text\r\n[<]: #\r\nOther text',
128
     clear: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\nOther text',
129
     full_win: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text',
130
     full_linux: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text'
131
   }, {
132
     name: 'Empty',
133
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n\r\n[<]: #\r\n',
134
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
135
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
136
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
137
   }, {
138
     name: 'Blank Line',
139
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
140
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
141
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
142
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
143
   }, {
144
     name: 'LF only',
145
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
146
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
147
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n',
148
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n'
149
   }, {
150
     name: 'add code fence 1',
151
     text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
152
     clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
153
     full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n',
154
     full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n'
155
   }, {
156
     name: 'add code fence 2',
157
     text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
158
     clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
159
     full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n',
160
     full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n'
161
   }, {
162
     name: 'add code fence 3',
163
     text: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
164
     clear: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
165
     full_win: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n',
166
     full_linux: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n'
167
   }, {
168
     name: 'Two replacements',
169
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
170
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
171
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
172
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
173
   }, {
174
     name: 'Code Fence 1',
175
     text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```',
176
     clear: '```json mdpInsert cat test/docs/abc.txt\r\n```',
177
     full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
178
     full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
179
   }, {
180
     name: 'Code Fence 2',
181
     text: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
182
     clear: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
183
     full_win: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
184
     full_linux: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
185
   }, {
186
     name: 'Code Fence and link',
187
     text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
188
     clear: '```json mdpInsert cat test/docs/abc.txt\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
189
     full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
190
     full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
191
   }, {
192
     name: 'Missing File',
193
     text: '[>]: # (mdpInsert cat file/not/present.txt)\r\n12345\r\n[<]: #\r\n',
194
     clear: '[>]: # (mdpInsert cat file/not/present.txt)\r\n[<]: #\r\n',
195
     full_win: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: type file\\not\\present.txt\r\nThe system cannot find the path specified.\r\n[<]: #\r\n',
196
     full_linux: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: cat file/not/present.txt\r\ncat: file/not/present.txt: No such file or directory\r\n[<]: #\r\n'
197
   }
198
 ]
199
200
 describe('unit tests', function () {
201
   describe('helpers.js', function () {
202
     describe('replaceLineEndings', function () {
203
       for (let i = 0; i < lineEndingTests.length; i++) {
204
         it(lineEndingTests[i].name + ' CRLF', function () {
205
           assert.equal(replaceLineEndings(lineEndingTests[i].text, true), lineEndingTests[i].resultCRLF)
206
         })
207
         it(lineEndingTests[i].name + ' LF', function () {
208
           assert.equal(replaceLineEndings(lineEndingTests[i].text, false), lineEndingTests[i].resultLF)
209
         })
210
       }
211
     })
212
     describe('findCode', function () {
213
       for (let i = 0; i < fencedCodeTests.length; i++) {
214
         it(fencedCodeTests[i].name, function () {
215
           let r = findCode(fencedCodeTests[i].text)
216
           delete r.info
217
           assert.deepEqual(r, fencedCodeTests[i].result)
218
         })
219
       }
220
     })
221
     describe('findMdpInsertBlocks', function () {
222
       for (let i = 0; i < mdpLinkTests.length; i++) {
223
         it(mdpLinkTests[i].name, function () {
224
           let r = findMdpInsert(mdpLinkTests[i].text)
225
           delete r.info
226
           assert.deepEqual(r, mdpLinkTests[i].result)
227
         })
228
       }
229
     })
230
     describe('findMdpCode', function () {
231
       for (let i = 0; i < mdpCodeTests.length; i++) {
232
         it(mdpCodeTests[i].name, function () {
233
           let r = findMdpCode(mdpCodeTests[i].text)
234
           delete r.info
235
           assert.deepEqual(r, mdpCodeTests[i].result)
236
         })
237
       }
238
     })
239
     describe('runCliCmd', function () {
240
       it('Simple', function () {
241
         assert.equal(runCliCmd('cat abc.txt', 'test/docs').toString(), 'abc')
242
       })
243
       it('invalid', function () {
244
         if (os.platform() === 'win32') {
245
           assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n')
246
         } else {
247
           assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n/bin/sh: 1: catt: not found\n')
248
         }
249
       })
250
     })
251
     describe('processText', function () {
252
       for (let i = 0; i < processTextTests.length; i++) {
253
         it(processTextTests[i].name + ' --clear', function () {
254
           let r = processText(processTextTests[i].text, true, process.cwd())
255
           assert.equal(r, processTextTests[i].clear)
256
         })
257
         it(processTextTests[i].name + ' full', function () {
258
           let r = processText(processTextTests[i].text, false, process.cwd())
259
           if (os.platform() === 'win32') {
260
             assert.equal(r, processTextTests[i].full_win)
261
           } else {
262
             assert.equal(r, processTextTests[i].full_linux)
263
           }
264
         })
265
       }
266
     })
267
     describe('processFile.js', function () {
268
       it('reference to processFile.js OK', function () {
269
         assert(processFile)
270
       })
271
       it('process file against missing file should error', function () {
272
         assert.throws(function () { processFile('not a file') }, /file not found/)
273
       })
274
     })
275
   })
276
 })
277
278
 describe('mdprepare', function () {
279
   it('mdprepare --help works', function (done) {
280
     this.timeout(8000)
281
     exec('mdprepare --help', function (error, stdout) {
282
       assert.ifError(error)
283
       assert.equal(stdout.toString(), fs.readFileSync('./test/help.txt').toString())
284
       done()
285
     })
286
   })
287
288
   it('fires and returns "processed x files message (1)"', function (done) {
289
     this.timeout(8000)
290
     exec('mdprepare --ignore=**/*.md', function (error, stdout) {
291
       assert.ifError(error)
292
       assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
293
       done()
294
     })
295
   })
296
297
   it('fires and returns "processed x files message (2)"', function (done) {
298
     this.timeout(8000)
299
     exec('mdprepare fred', function (error, stdout) {
300
       assert.ifError(error)
301
       assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
302
       done()
303
     })
304
   })
305
306
   it('fires and returns error if directory not found', function (done) {
307
     this.timeout(8000)
308
     exec('mdprepare //', function (error, stdout, stderr) {
309
       assert.ifError(error)
310
       assert.notEqual(stderr.toString(), '')
311
       done()
312
     })
313
   })
314
 })
315
316
 describe('mdprepare test files', function () {
317
   fs.emptyDirSync('./test/preparedClear')
318
   fs.copySync('./test/docs', './test/preparedClear')
319
   fs.emptyDirSync('./test/preparedFull')
320
   fs.copySync('./test/docs', './test/preparedFull')
321
   var files = fs.readdirSync('./test/docs')
322
   for (let i = 0; i < files.length; i++) {
323
     if (files[i].substr(-3) === '.md') {
324
       it('/test/docs/' + files[i] + ' is prepared correctly', function (done) {
325
         this.timeout(8000)
326
         exec('mdprepare ./test/preparedFull/' + files[i], function (error) {
327
           assert.ifError(error)
328
           assert.equal(fs.readFileSync('./test/preparedFull/' + files[i], 'utf8'), fs.readFileSync('./test/preparedFull/' + files[i] + '.full', 'utf8'))
329
           done()
330
         })
331
       })
332
       it('/test/docs/' + files[i] + ' is cleared correctly', function (done) {
333
         this.timeout(8000)
334
         exec('mdprepare ./test/preparedClear/' + files[i] + ' --clear', function (error) {
335
           assert.ifError(error)
336
           assert.equal(fs.readFileSync('./test/preparedClear/' + files[i], 'utf8'), fs.readFileSync('./test/preparedClear/' + files[i] + '.clear', 'utf8'))
337
           done()
338
         })
339
       })
340
     }
341
   }
342
   it('test working with read only file - should error', function (done) {
343
     this.timeout(8000)
344
     fs.writeFileSync('./test/preparedClear/readonly.md', '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n')
345
     fs.chmodSync('./test/preparedClear/readonly.md', 0o444) // make file read only
346
     exec('mdprepare ./test/preparedClear/readonly.md', function (error, stdout, stderr) {
347
       assert.ifError(error)
348
       assert.notEqual(stderr.toString(), '')
349
       done()
350
     })
351
   })
352
 })
353